﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace homework_8th
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.textBoxInput.Text = "请输入";
            this.textBoxResult.Text = "无结果";
        }

        /// <summary>
        /// 计算按钮，
        /// 将会调用Calculate.checkBrackets对算式进行检测，
        /// 将会调用Calculate.calculate计算算式结果，
        /// 并捕获相关异常，反映到结果中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonConfirm_Click(object sender, EventArgs e)
        {
            if (this.textBoxResult.Text.Equals("无结果"))
                this.textBoxResult.Text = "";
            try
            {
                Calculate.checkBrackets(this.textBoxInput.Text);
                if (this.textBoxInput.Text[textBoxInput.Text.Length - 1] != '=')
                    this.textBoxResult.Text += (this.textBoxInput.Text
                        + " = "
                        + Calculate.calculate(this.textBoxInput.Text).ToString()
                        + "\r\n");
                else
                    this.textBoxResult.Text += (this.textBoxInput.Text
                        + Calculate.calculate(this.textBoxInput.Text).ToString()
                        + "\r\n");
            }
            catch (Exception)
            {
                this.textBoxResult.Text = "输入格式有误";
            }
        }
        
        /// <summary>
        /// 清除按钮，将会初始化两个文本框的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonClear_Click(object sender, EventArgs e)
        {
            this.textBoxInput.Text = "请输入";
            this.textBoxResult.Text = "无结果";
        }
    }
}
